package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmUserRoleRel;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmUserRoleRelCopyMapper;
//import cn.com.yusys.yusp.admin.sso.SSOHandlerOp;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import cn.com.yusys.yusp.uaa.domain.DomainUser;
import cn.com.yusys.yusp.uaa.repository.mapper.DomainUserDetailsMapper;
import com.ctrip.framework.apollo.util.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/api/sznshauth")
public class LoginResource {

    @Autowired
//    private SSOHandlerOp ssoHandlerOp;

    @Resource
    private DomainUserDetailsMapper domainUserDetailsMapper;

    @Resource
    private AdminSmUserRoleRelCopyMapper adminSmUserRoleRelCopyMapper;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @GetMapping("/checktoken")
    public ResultDto<Map<String, String>> checktoken(HttpServletRequest request) throws IOException {
//        String sso = ssoHandlerOp.sso(ssoToken);
        String sso = "";
//        HttpServletRequest request = (HttpServletRequest)arg0;
        HttpSession httpSession = request.getSession();
        String userId = (String) httpSession.getAttribute("loginName");
        ResultDto<Map<String, String>> resultDto = new ResultDto<>();
        if (("failure").equals(sso)) {
            resultDto.setCode(5000);
            resultDto.setMessage("Token验证失败，请联系管理员处理");
        } else {
            //验证用户是否存在
            DomainUser userFromDatabase = this.domainUserDetailsMapper.selectUserByLoginCode(userId);
            if(StringUtils.isEmpty(userFromDatabase)){
                resultDto.setCode(5000);
                resultDto.setMessage("网格化营销系统中没有此用户，请联系管理员处理");
            }else{
                List<AdminSmUserRoleRel> userRoleRelList= adminSmUserRoleRelCopyMapper.getUserRoleByUserId(userId);
                if(userRoleRelList != null && !userRoleRelList.isEmpty()){
                    Map<String, String> map = new HashMap<>();
                    map.put("username",userId);
                    map.put("password","true");
                    resultDto.setData(map);
                }else{
                    resultDto.setCode(5000);
                    resultDto.setMessage("网格化营销系统中此用户没有分配角色，请联系管理员处理");
                }
            }
        }
        return resultDto;
    }

}